//
//  AppDelegate.m
//  GiftThat
//
//  Created by ChuXiang on 16/1/6.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "AppDelegate.h"
#import "UMSocial.h"
#import "JPUSHService.h"
@interface AppDelegate ()
@property (nonatomic, strong) UITabBarController *tabBarController;
@property (nonatomic,retain) SFYDYViewController *YDYViewController;
@end

@implementation AppDelegate
-(void)ChooseVC{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
        _window.rootViewController = self.YDYViewController;
        
        
    }else{
        
        self.tabBarController.tabBar.tintColor = [UIColor redColor];
        _window.rootViewController = self.tabBarController;
    }
    
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
   
    [UMSocialData setAppKey:@"507fcab25270157b37000010"];

    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    UIImage *giftThatNoSelect = [UIImage imageNamed:@"GiftThatNoSelect"];
    UIImage *giftThatSelect = [UIImage imageNamed:@"GiftThatSelect"];
    
    UIImage *hotTopicsNoSelect = [UIImage imageNamed:@"HotTopicsNoSelect"];
    UIImage *hotTopicsSelect = [UIImage imageNamed:@"HotTopicsSelect"];
    
    UIImage *classificationNoSelect = [UIImage imageNamed:@"ClassificationNoSelect"];
    UIImage *classificationSelect = [UIImage imageNamed:@"ClassificationSelect"];
    
    UIImage *mineNoSelect = [UIImage imageNamed:@"MineNoSelect"];
    UIImage *mineSelect = [UIImage imageNamed:@"MineSelect"];
    
    GiftThatViewController *giftThatVc = [GiftThatViewController new];
    UINavigationController *giftThatNav = [[UINavigationController alloc] initWithRootViewController:giftThatVc];
    giftThatVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"礼物说" image:giftThatNoSelect selectedImage:[giftThatSelect imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    HotTopicsViewController *hotTopicsVc = [HotTopicsViewController new];
    UINavigationController *hotTopicsNav = [[UINavigationController alloc] initWithRootViewController:hotTopicsVc];
    hotTopicsVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"热门" image:hotTopicsNoSelect selectedImage:[hotTopicsSelect imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    ClassiFicationViewController *classiFicationVc = [ClassiFicationViewController new];
    UINavigationController *classiFicationNav = [[UINavigationController alloc] initWithRootViewController:classiFicationVc];
    classiFicationVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分类" image:classificationNoSelect selectedImage:[classificationSelect  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    MineViewController *mineVc = [MineViewController new];
    UINavigationController *mineNav = [[UINavigationController alloc] initWithRootViewController:mineVc];
    mineVc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:mineNoSelect selectedImage:[mineSelect  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    self.tabBarController = [UITabBarController new];
    self.tabBarController.viewControllers = @[giftThatNav, hotTopicsNav, classiFicationNav, mineNav];
    self.tabBarController.tabBar.tintColor = [UIColor colorWithRed:234 / 255.0 green:74 / 255.0 blue:75 / 255.0 alpha:1.0];
    self.tabBarController.tabBar.translucent = NO;
     self.YDYViewController = [[SFYDYViewController alloc]init];
    self.YDYViewController.delegate = self;
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        _window.rootViewController = self.YDYViewController;

        
    }else{
  
        _window.rootViewController = self.tabBarController;
    }
   

    
    //开启缓存
    NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:5 * 1024 * 1024 diskCapacity:25 * 1024 * 1024 diskPath:nil];
    [NSURLCache setSharedURLCache:cache];
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //categories
        [JPUSHService
         registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                             UIUserNotificationTypeSound |
                                             UIUserNotificationTypeAlert)
         categories:nil];
    } else {
        //categories nil
        [JPUSHService
         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                             UIRemoteNotificationTypeSound |
                                             UIRemoteNotificationTypeAlert)
#else
         //categories nil
         categories:nil];
        [APService
         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                             UIRemoteNotificationTypeSound |
                                             UIRemoteNotificationTypeAlert)
#endif
         // Required
         categories:nil];
    }
    [JPUSHService setupWithOption:launchOptions];
    

    
    return YES;
}

//注册成功 返回信息
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken{
    NSLog(@"%@",deviceToken);
    [JPUSHService registerDeviceToken:deviceToken];
}
//注册失败 返回错误信息
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"%@",error);
}

//收到的远程推送的内容
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo{
    NSLog(@"%@",userInfo);
    [JPUSHService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void
                        (^)(UIBackgroundFetchResult))completionHandler {
    
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
